package com.tanhua.dubbo.api;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.dubbo.mongo.UserLikeApi;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, String likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike one = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (one == null) {
                one = new UserLike();
                one.setUserId(userId);
                one.setLikeUserId(Long.valueOf(likeUserId));
                one.setIsLike(isLike);
                one.setCreated(System.currentTimeMillis());
                one.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(one);
            } else {

                //3、更新
                Update update = Update.update("isLike", isLike).set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    //查询喜欢
    @Override
    public List<UserLike> loveCounts(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true).and("likeUserId").nin(userId);
        List<UserLike> list = mongoTemplate.find(Query.query(criteria), UserLike.class);
        return list;
    }

    //查询粉丝
    @Override
    public List<UserLike> fanCounts(Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true).and("userId").nin(userId);
        List<UserLike> list = mongoTemplate.find(Query.query(criteria), UserLike.class);
        return list;
    }

    //查询互相喜欢
    @Override
    public List<UserLike> eachLoveCounts(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true).and("likeUserId").nin(userId);
        List<UserLike> userLikes = mongoTemplate.find(Query.query(criteria), UserLike.class);
        List<UserLike> list = new ArrayList<>();
        for (UserLike userLike : userLikes) {
            Criteria id = Criteria.where("userId").is(userLike.getLikeUserId()).and("likeUserId").is(userId).and("isLike").is(true);
            UserLike one = mongoTemplate.findOne(Query.query(id), UserLike.class);
            if (ObjectUtil.isNotEmpty(one)) {
                list.add(one);
            }
        }
        return list;
    }
 //是否喜欢
    @Override
    public boolean findByIdAndLikeId(Long userId, Long aLong) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(aLong);
        UserLike one = mongoTemplate.findOne(Query.query(criteria), UserLike.class);
        if (ObjectUtil.isNotEmpty(one)){
            return true;
        }
        return false;
    }

    @Override
    public List<UserLike> love(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true).and("likeUserId").nin(userId);
        List<UserLike> list = mongoTemplate.find(Query.query(criteria).with(Sort.by(Sort.Order.desc("created")))
                .limit(pagesize).skip((page - 1) * pagesize), UserLike.class);
        return list;
    }

    @Override
    public List<UserLike> fan(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true).and("userId").nin(userId);
        List<UserLike> list = mongoTemplate.find(Query.query(criteria).with(Sort.by(Sort.Order.desc("created")))
                .limit(pagesize).skip((page - 1) * pagesize), UserLike.class);
        return list;
    }

    @Override
    public List<UserLike> eachLove(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true).and("likeUserId").nin(userId);
        List<UserLike> userLikes = mongoTemplate.find(Query.query(criteria), UserLike.class);
        List<UserLike> list = new ArrayList<>();
        for (UserLike userLike : userLikes) {
            Criteria id = Criteria.where("userId").is(userLike.getLikeUserId()).and("likeUserId").is(userId).and("isLike").is(true);
            UserLike one = mongoTemplate.findOne(Query.query(id).with(Sort.by(Sort.Order.desc("created")))
                    .limit(pagesize).skip((page - 1) * pagesize), UserLike.class);
            if (ObjectUtil.isNotEmpty(one)) {
                list.add(one);
            }
        }
        return list;
    }
}
